package com.coincalf.wallet.web.result;

import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

/**
 * @author qiang
 */
@RestControllerAdvice
public class HttpResponseBodyAdvice implements ResponseBodyAdvice<Object> {

    @Override
    public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
        return FastJsonHttpMessageConverter.class.isAssignableFrom(converterType);
    }

    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
        return body;
        /*if (body instanceof ActionResult || body instanceof String) {
            return body;
        } else if (body instanceof File) {
            return body;
        } else {
            log.debug("MyResponseBodyAdvice==>beforeBodyWrite:" + returnType + "," + body);
            ActionResult result = new ActionResult(true);
            result.setCode(StatusCode.OK);
            result.setData(body);
            body = (Object) result;
            // body.setB("我是后面设置的");
            return body;
        }*/
    }
}
